package com.example.reductionnotice;

import java.io.IOException;
import java.io.StringReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class ItemQuery {

	private static ItemQuery item_ = null;
	private String numberId_;
	public ItemQuery(){
		
	}
	
	public static ItemQuery instance(){
		if (item_ == null)		
			item_ = new ItemQuery();
		
		return item_;		 
	}
	
	public ItemInfo query(String numid){
		
		numberId_ = numid;
		return this.parse(this.download(numid));
		
	}
	
	private String download(String numid){		
		
		DatagramSocket ds = null;		
		DatagramPacket dp = null, dp2 = null;
		 
		try {  
			ds = new DatagramSocket();  
		} catch (SocketException e)
		{  
			e.printStackTrace();  
		}  

		try {  
			String nid =numid +";";  
			byte[] bs = nid.getBytes();
			dp = new DatagramPacket(bs,bs.length,InetAddress.getByName("42.121.236.245"),8000);  
		} catch (UnknownHostException e)
		{ 
			e.printStackTrace();  
		}  

		try {  
			ds.send(dp);  	                   
		} catch (IOException e) {  

			e.printStackTrace();  
		}  
       
       
       byte [] data = new byte[8192];  
       dp2 = new DatagramPacket(data, data.length);  
       
       try {  
    	   ds.receive(dp2);     	  	
    	   
//    	   int index = content.indexOf("encoding=");
//
//    	   if (index == -1){
//    		   StringBuilder builder = new StringBuilder(content);
//
//    		   builder.insert(19 , " encoding=\"UTF-8\"");
//
//    		   //content = builder.toString();
//    	   }

       } catch (IOException e) {  
    	   e.printStackTrace();  
       }   
       
       return (new String(data));
	}
	
	private ItemInfo parse(String content)
	{
		ItemInfo  iteminfo = new ItemInfo();			
		 
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); 
        
		DocumentBuilder db=null; 
		try{ 
			db=dbf.newDocumentBuilder(); 
		}catch(ParserConfigurationException pce) 
		{ 
			System.err.println(pce); 	            		 
		} 
		
		Document doc=null; 
		
		try{ 
			doc=db.parse(new InputSource(new StringReader(content))); 
		}catch(Exception e) { 
			System.err.println(e);    
		} 		

		Element root=doc.getDocumentElement();
		NodeList nodes1 =root.getElementsByTagName("title");

		if (nodes1.getLength() != 0){
			iteminfo.setName(nodes1.item(0).getTextContent());
		}

		
		NodeList nodes2 =root.getElementsByTagName("price");
		
		if (nodes2.getLength() != 0){
			iteminfo.setCurPrice(nodes2.item(0).getTextContent());
		}   	            		
        
		iteminfo.setNumberId(numberId_);
		
		return iteminfo;	
	}
}
